import os
from lib.peps_download import PepsDownload
import concurrent.futures
# from lib.utils import province2tiles

'''
按照省名或图幅号、开始时间、结束时间从PEPS下载数据
'''


def downloader(options):
    PepsDownload(collection=options['collection'],
                 auth=options['auth'],
                 tile=options['tile'],
                 start_date=options['start_date'],
                 search_json_file=options['search_json_file'],
                 end_date=options['end_date'],
                 write_dir=options['write_dir'],
                 no_download=options['no_download'],
                 )


def data_download(l1c_dir, tiles, start_date, end_date, max_workers=10, no_download=False):
    """
    使用多进程进行数据下载
    :param l1c_dir: l1c数据路径
    :param tiles: 图幅号
    :param start_date: 开始日期 :  '2021-09-01'
    :param end_date: 结束日期 :  '2021-10-01'
    :param max_workers: 使用的进程数量 : 16
    :param no_download: 是否不下载数据，为False时进行数据下载
    :return:
    """
    options_list = []
    for tile in tiles:
        search_json = l1c_dir + '/' + tile + '_' + start_date + '.json'
        options_list.append(dict(collection='S2ST',
                                 auth='./conf/peps.txt',
                                 tile=tile,
                                 start_date=start_date,
                                 search_json_file=search_json,
                                 end_date=end_date,
                                 write_dir=l1c_dir,
                                 no_download=no_download,
                                 )
                            )

    with concurrent.futures.ProcessPoolExecutor(max_workers=max_workers) \
            as executor:
        for options in options_list:
            executor.submit(downloader, options)


def download_province_sentinel(l1c_dir,
                               start_date,
                               end_date,
                               province,
                               max_workers=10,
                               no_download=False
                               ):
    """
    按照省下载哨兵数据
    :param l1c_dir: l1c路径 :'/data/Sentinel2/1C/DongSanSheng'
    :param start_date: 开始日期 : '2021-09-01'
    :param end_date: 结束日期 : '2021-10-01'
    :param province: 省名 : '辽宁省'
    :param max_workers: 使用的进程数量 : 16
    :param no_download: 是否不下载数据，为False时进行数据下载
    :return:
    """
    # tiles = province2tiles(province)
    # tiles=["43SED","43SFD",'43SGD','43SFC','43SGC','44SKJ','44SKG','44SLG','44SMG','44SNF','44TLL','44TML']
    tiles = "43SED-43SFD-43SGD-43SFC-43SGC-44SKJ-44SKG-44SLG-44SMG-44SNF-44TLL-44TML-44TNL-44TPL-44TQL-44TLK-44TMK-44TNK-45SUC-45TTF-45TUF-45TVF-45TWF-44SLJ"
    tiles = tiles.split("-")
    if not os.path.exists(l1c_dir):
        os.makedirs(l1c_dir)
    data_download(l1c_dir, tiles, start_date, end_date, max_workers, no_download)


# 按照省名、开始时间、结束时间下载数据
def main_province():
    l1c_dir=r'E:\Yang\新疆'
    start_date = '2021-06-01'
    end_date = '2021-07-01'
    province = '新疆'
    no_download = False
    download_province_sentinel(l1c_dir,
                               start_date,
                               end_date,
                               province,
                               16,
                               no_download)


# 按照图幅号、开始时间结束时间、下载数据,
# 主要用于补充缺少的数据，因此将进程数设置为图幅数+1
def main_tiles():
    no_download = False
    # tiles = ['51UWV', '51UWT', '51UYT', '52TGS', '52TDR', '52TEP', '52UCA', '52UCV', '52UFU', '53TLN', '53ULP', '53UMP',
    #          '53UNP']
    # tiles = ['51TWN', '51TXN', '52UEV', '52UEU']
    tiles = ['51TYK', '51TVJ', '51TXJ', '51TYJ', '51TXH', '51TYH', '51TXG', '51TYG', '52TCR', '52TCN', '52TDN', '52TEN', '52TFN', '52TCM', '52TDM', '52TEM', '52TFM', '52TBL', '52TCL', '52TDL', '52TBM']

    l1c_dir = '/data/Sentinel2/1C/DongSanSheng'
    start_date = '2021-05-01'
    end_date = '2021-06-01'
    data_download(l1c_dir, tiles, start_date, end_date, 16, no_download)


if __name__ == '__main__':
    main_province()
    # filename='/83To82/all_data/jiang_su/2022_02/flag.txt'
    # if not os.path.exists(filename):
    #     os.system(r'touch %s' % filename)

